package ynu.edu.api.client;

import io.github.resilience4j.ratelimiter.RequestNotPermitted;
import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import ynu.edu.api.client.fallback.PermissionFeignClientFallbackFactory;

import java.util.Collections;
import java.util.List;

@FeignClient(
        name = "auth-service",
        fallbackFactory = PermissionFeignClientFallbackFactory.class
)
public interface PermissionFeignClient {

    @GetMapping("/auth/permissions")
    @RateLimiter(name = "feignRateLimiter")  // 移除fallbackMethod，使用FallbackFactory处理
    List<String> getPermissions(@RequestParam("userId") Long userId);

    @GetMapping("/auth/roles")
    List<String> getRoles(@RequestParam("userId") Long userId);

    @PostMapping("/auth/default-role")
    void assignDefaultRole(@RequestParam("userId") Long userId);

    // 定义降级方法（需与原始方法签名一致）
    default List<String> getPermissionsFallback(Long userId, RequestNotPermitted ex) {
        return Collections.emptyList(); // RateLimiter触发的降级
    }
}
